package com.jfree.nettyshow.client.handler;

import com.jfree.netty.MyProtocolBean;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

import java.net.InetSocketAddress;

/**
 * @author wangjian
 * @date 2019/10/23 17:08
 */

public class TimeServerHandler extends ChannelInboundHandlerAdapter {

    //TODO netty bytebuf retain???
    //TODO  ctx.executor()???
    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        System.out.println("  ctx.channel().eventLoop()="+  ctx.channel().eventLoop());
        System.out.println("ctx thread name:"+Thread.currentThread().getName());
        ByteBuf buf = (ByteBuf) msg;
        // TODO ctx.channel().attr() https://blog.csdn.net/linuu/article/details/51502136
        //TODO 到了这里如果msg是ByteBuf，最好再判断字节是否正确防止处理错误数据
        if (msg != null) {
            int length = buf.readInt();
            System.out.println("length=" + length);

            System.out.println("buf.readableBytes()=" + buf.readableBytes());
            if (length != buf.readableBytes()) {
                throw new Exception("数据字节错误");
            }
            byte[] req = new byte[buf.readableBytes()];
            buf.readBytes(req);
            String body = new String(req);
            System.out.println("---message=" + body);
            String le = "{Hello,netty}";
            System.out.println("thread-name:" + Thread.currentThread().getName());
            MyProtocolBean myProtocolBean = new MyProtocolBean((byte) 10, (byte) 12, le.length(), le);
            //TODO netty Netty事件监听和处理,测试时同步？？？,再controller中监听就是两个线程，貌似和发送一样不在netty线程中，则封装成task给netty线程执行
            System.out.println("-----sleep----");
            ctx.fireChannelRead(msg);
        }

    }

    @Override
    public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
        //获取客服端地址
        InetSocketAddress insocket = (InetSocketAddress) ctx.channel().remoteAddress();
        System.out.println(insocket.getHostName() + "--" + insocket.getHostString() + "--" + insocket.getPort() + "--");
        System.out.println(insocket.getAddress().getCanonicalHostName() + "--" + insocket.getAddress().getHostAddress() + "--" + insocket.getAddress().getHostName() + "--add" + new String(insocket.getAddress().getAddress()));
        System.out.println("channel name：" + ctx.channel().localAddress().toString() + "---id=" + ctx.channel().id());
        System.out.println("TIMR-----");
        ctx.fireChannelReadComplete();
        ctx.channel().remoteAddress();

    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause)
            throws Exception {
        System.out.println("cause=" + cause.toString());
        //  ctx.fireExceptionCaught(cause);
    }

    @Override
    public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
        System.out.println("userEventTriggereduserEventTriggereduserEventTriggered");
        ctx.fireUserEventTriggered(evt);
    }

}
